package com.changgou.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.InetSocketAddress;

@Component
public class IpFilter implements GlobalFilter, Ordered {
    //具体的逻辑业务
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取客户端的访问ip
        System.out.println("经历了优先权为1的过滤器");
        ServerHttpRequest request = exchange.getRequest();
        InetSocketAddress remoteAddress = request.getRemoteAddress(); //获取远程地址
        String hostName = remoteAddress.getHostName(); //获取访问主机名
        System.out.println("ip" + hostName);
        //放行
        return chain.filter(exchange);
    }

    //过滤器的执行优先权,返回值越小,越先执行
    @Override
    public int getOrder() {
        return 1;
    }
}
